﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ProjectPigs.Assets;
using ProjectPigs.Core;
using ProjectPigs.Cameras;

namespace ProjectPigs.Scenes
{
    public class GameScene : Scene
    {
        Texture2D[] bgLayers;

        Strip3D midLayerStrip;
        Strip3D frontLayerStrip;

        BaseCamera[] cameras;
        PPLight[] lights;
   

        private int meshCount;

        public int MeshCount { get { return meshCount; } }

        public static BaseCamera ActiveCamera;

        public GameScene(String name, Map map)
            :base(name)
        {
            bgLayers = new Texture2D[3];

            midLayerStrip = new MidLayerStrip();
            frontLayerStrip = new FrontLayerStrip();

            addMapToMidLayer(map);

        }

        public void addMapToMidLayer(Map map)
        {
            if (midLayerStrip.IsEmpty)
            {
                extractMap(midLayerStrip, map);
            }
            else
            {
                //Debug.pushEntry("WARNING: Map cannot be added to midlayer more than once", 3);
            }
        }

        public void addMapToFrontLayer(Map map)
        {
            extractMap(frontLayerStrip, map);
        }

        public void addGameObject(GameObject gameObject)
        {
            this.gObjects.Add(gameObject);
        }

        protected override void start()
        {
        }

        public override void load(Microsoft.Xna.Framework.Content.ContentManager content)
        {
        }

        public override void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {
            foreach (GameObject obj in gObjects)
            {
                if (obj is RenderableGameObject)
                {
                    (obj as RenderableGameObject).draw(sb);
                }
            }

            midLayerStrip.draw();
        }

        public override void update(Microsoft.Xna.Framework.GameTime time)
        {

            ActiveCamera.update(time);

            foreach (GameObject obj in gObjects)
            {
                if (obj is DynamicGameObject)
                {
                    (obj as DynamicGameObject).update(time);

                }
            }

            foreach (PPLight light in lights)
            {
                light.update(time);
            }




        }

        private void extractMap(Strip3D strip, Map map)
        {

            foreach (GeneralAsset asset in map.GeneralAssets)
            {
                Console.WriteLine("extr " + asset.Name);

                if (asset.Type.ToLower() == "light")
                {

                }
                else if (asset.Type.ToLower() == "camera")
                {

                }
                else if (asset.Type == "Mesh")
                {
                    Console.WriteLine("mesh being extracted [" + asset.Name + "]");
                    if (AssetManager.findMesh(asset.Name) != null)
                    {
                        strip.addMesh(new PPModel(asset.Transform));
                        meshCount++;
                    }
                }
                else if (asset.Type.ToLower() == "collider")
                {

                }
            }
        }
    }

    
}
